{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 21\n",
    "\n",
    "# 判断正定矩阵\n",
    "Book_4《矩阵力量》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54195758-c635-45fc-be6a-ebae54b12d78",
   "metadata": {},
   "source": [
    "这段代码的功能是判断一个矩阵是否为正定矩阵。首先，正定矩阵 \\( A \\) 的定义要求其必须是对称矩阵，即满足 \\( A = A^T \\)。若矩阵 \\( A \\) 是对称矩阵，代码进一步检查是否可以对其进行 Cholesky 分解。Cholesky 分解是一种将正定矩阵 \\( A \\) 表示为下三角矩阵 \\( L \\) 和其转置 \\( L^T \\) 的操作，即\n",
    "\n",
    "$$\n",
    "A = L L^T\n",
    "$$\n",
    "\n",
    "若分解成功，则说明 \\( A \\) 是正定矩阵，函数返回 `True`；若分解失败（引发 `LinAlgError` 异常），则矩阵不是正定矩阵，函数返回 `False`。在这段代码中，示例矩阵\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix} 1 & 0 \\\\ 0 & 0 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "不是正定矩阵，因此代码会输出 `False`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "53cb4618-5e9f-4388-b8e2-893534f3cfbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入 numpy 进行数值计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74d9b36f-c406-4573-a99e-db75e9380e36",
   "metadata": {},
   "source": [
    "## 定义判断正定矩阵的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7abef436-2925-4561-90ae-dc85e1fd34f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_pos_def(A):  # 函数 is_pos_def 用于判断矩阵是否为正定矩阵\n",
    "    if np.array_equal(A, A.T):  # 检查矩阵是否为对称矩阵\n",
    "        try:\n",
    "            np.linalg.cholesky(A)  # 尝试进行 Cholesky 分解\n",
    "            return True  # 若成功，则矩阵为正定矩阵\n",
    "        except np.linalg.LinAlgError:  # 若分解失败，捕获异常\n",
    "            return False  # 分解失败则矩阵不是正定矩阵\n",
    "    else:\n",
    "        return False  # 若矩阵不对称，则直接返回 False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7632f259-4cc2-40b5-8886-5c2c670b876c",
   "metadata": {},
   "source": [
    "## 定义待检测的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "11933a91-89ae-4529-b5f6-302fa7eebf2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 0], \n",
    "              [0, 0]])  # 定义矩阵 A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7bb2c23d-c6ef-4f9a-8c48-01f55905fa97",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 打印结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6f4d7b05-2a48-40b5-879a-12e58b6ff62a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(is_pos_def(A))  # 调用函数 is_pos_def，打印矩阵 A 是否为正定矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
